//package com.java.base.exercise.proxy;
//import com.java.base.exercise.proxymoudle.IUserApi;
//import com.java.base.exercise.proxymoudle.UserApi;
//import jdk.internal.org.objectweb.asm.Opcodes;
//import jdk.internal.org.objectweb.asm.commons.AdviceAdapter;
//import org.objectweb.asm.ClassReader;
//import org.objectweb.asm.ClassVisitor;
//import org.objectweb.asm.ClassWriter;
//import org.objectweb.asm.MethodVisitor;
//
//import java.io.IOException;
//
//import static jdk.internal.org.objectweb.asm.Opcodes.ASM5;
//
///**
// * @Author RenPu
// * @Date 2022/7/13 16:27
// * @Version 1.0
// * @Description:
// **/
//public class ASMProxy extends ClassLoader {
//
//    public static  <T> T getProxy(Class clazz) throws Exception {
//        ClassReader classReader = new ClassReader(clazz.getName());  //类读取对象
//        ClassWriter classWriter = new ClassWriter(classReader, ClassWriter.COMPUTE_MAXS); //类写入对象
//
//        classReader.accept(new ClassVisitor(ASM5,classWriter) {
//            @Override
//            public MethodVisitor visitMethod(int access, String name, String descriptor, String signature, String[] exceptions) {
//                if (!"queryUserInfo".equals(name)) return super.visitMethod(access, name, descriptor, signature, exceptions);
//
//                final MethodVisitor methodVisitor = super.visitMethod(access, name, descriptor, signature, exceptions);
//
//                return new AdviceAdapter(ASM5,methodVisitor,access,name,descriptor){
//                    @Override
//                    protected void onMethodEnter() {
//                        // 执行指令；获取静态属性
//                        methodVisitor.visitFieldInsn(Opcodes.GETSTATIC, "java/lang/System", "out", "Ljava/io/PrintStream;");
//                        // 加载常量 load constant
//                        methodVisitor.visitLdcInsn(name + " 你被代理了，By ASM！");
//                        // 调用方法
//                        methodVisitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/io/PrintStream", "println", "(Ljava/lang/String;)V", false);
//                        super.onMethodEnter();
//                    }
//
//                };
//
//                }
//        },ClassReader.EXPAND_FRAMES);
//
//
//        byte[] bytes = classWriter.toByteArray();
//        return (T) new ASMProxy().defineClass(clazz.getName(), bytes, 0, bytes.length).newInstance();
//
//
//    }
//
//
//    public static void main(String[] args) {
//        IUserApi userApi = ASMProxy.getProxy(UserApi.class);
//        String invoke = userApi.queryUserInfo();
//        System.out.println("测试结果："+invoke);
//    }
//
//
//
//}
